#include "mergedialog.h"

#include <QGridLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QSpacerItem>
#include <QDir>
#include <QFileDialog>
#include <QByteArray>
#include <QImageReader>


MergeDialog::MergeDialog(QWidget *parent) :
    QDialog(parent)
{
    dad = (MainWindow*)parent;

    setAttribute(Qt::WA_DeleteOnClose);

    QVBoxLayout * mainLayout = new QVBoxLayout(this);
    QGridLayout * topLayout = new QGridLayout();
    QGridLayout * buttonLayout = new QGridLayout();

    QLabel *subtitle = new QLabel(tr("Merge with another image"));
    QFont font = QFont();
    font.setBold(true);
    font.setPointSize(14);
    subtitle->setFont(font);

    QLabel *label1 = new QLabel(tr("Other Image"), this);
    file = new QLineEdit(this);
    QPushButton *fileButton = new QPushButton(tr("Set File"), this);


    QLabel *label2 = new QLabel(tr("Coefficient"), this);
    spinCoef = new QDoubleSpinBox(this);
    spinCoef->setMaximum(1.0);
    spinCoef->setMinimum(0.0);
    spinCoef->setValue(0.5);
    spinCoef->setSingleStep(0.01);

    QLabel *infoLabel = new QLabel(tr("Note: If the second image is bigger <br>"
                                      "than the original, you will have to resize <br>"
                                      "it (if desired) by using the selection box <br>"
                                      "that appears in the next step."));
    QFont fontInfo = QFont();
    fontInfo.setItalic(true);
    fontInfo.setPointSize(10);
//    fontInfo.setWeight(Qt);
    infoLabel->setFont(fontInfo);

    cancelButton = new QPushButton(tr("&Cancel"), this);
    mergeButton = new QPushButton(tr("&Merge"), this);
    mergeButton->setDefault(true);

    topLayout->addWidget(label1, 0, 0);
    topLayout->addWidget(file, 0, 1);
    topLayout->addWidget(fileButton, 0, 2);
    topLayout->addWidget(label2, 1, 0);
    topLayout->addWidget(spinCoef, 1, 1);

    buttonLayout->addWidget(cancelButton, 0, 1);
    buttonLayout->addWidget(mergeButton, 0, 2);

    QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);

    mainLayout->addWidget(subtitle);
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(infoLabel);
    mainLayout->addSpacerItem(spacer);
    mainLayout->addLayout(buttonLayout);

    connect(fileButton, SIGNAL(clicked()), this, SLOT(setFile()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(mergeButton, SIGNAL(clicked()), this, SLOT(acceptMerge()));

    fileName = QString();
}

void MergeDialog::setFile()
{
    QStringList formats;
    foreach (QByteArray format, QImageReader::supportedImageFormats())
        if (format.toLower() == format)
            formats.append("*." + format);
    formats.append("*.pnm");

    fileName = QFileDialog::getOpenFileName(this,
                                    tr("Open Image"),
                                    QDir::homePath(),
                                    tr("Image Files (%1)").arg(formats.join(" ")));

    if (!fileName.isEmpty()) {
        file->setText(fileName);
    }
}

void MergeDialog::acceptMerge()
{
    QFileInfo fileInfo(fileName);
    if (!fileInfo.isFile()) {
        QMessageBox::information(this, tr("No file set"),
                                 tr("You haven't set any file to merge with.<br>"
                                    "Please set one, or cancel the merge."));
    }
    else {
        emit merge(fileName, spinCoef->value());
        accept();
    }
}

